// ----------------------------------------
// Filename: Signals.c
// Description: Code providing an abstraction for 
//				hardware and/or software interrupts
// Author: Jon Maloney
// Date: 8-Jun-2012
//

//************************************************************
//                    Include .h files
//************************************************************
#include "Signals.h"
#include "microRTOS.h"
#include "p24FJ64GA004.h"
#include "binary_Semaphore.h"
//************************************************************
//                    Include external references
//************************************************************

//************************************************************
//                    Define Statements
//************************************************************
#define clearFLAG 0
//************************************************************
//                    Struct, Union, enum Statements
//************************************************************

//************************************************************
//                    Global variables and constants
//************************************************************

bsem_Handle ADC;
bsem_Handle UART2Rx;
bsem_Handle UART2Tx;

//************************************************************
//                    Prototype Private Functions
//************************************************************

//************************************************************
//                    Implementation
//************************************************************


void __attribute__((__interrupt__, auto_psv)) _ADC1Interrupt( void )
{
	/* Clear the ADC Done interrupt. */
	_AD1IF = clearFLAG;
	AD1CON1bits.ASAM = 0;	//Stop sampling wait for the timer to interrupt to start sampling again
	ENABLE_INTERRUPTS();

	//Signal the ADC thread that the ADC has finished sampling
	Semaphore_Signal(ADC); //Note null references are handled internally
}

void __attribute__((__interrupt__, auto_psv)) _U2RXInterrupt( void )
{
	IFS1bits.U2RXIF = clearFLAG;

	ENABLE_INTERRUPTS();

	//Signal the UART RX thread that a new byte is available
	Semaphore_Signal(UART2Rx);//Note null references are handled internally
}

void __attribute__((__interrupt__, auto_psv)) _U2TXInterrupt( void )
{
	IFS1bits.U2TXIF = clearFLAG;

	ENABLE_INTERRUPTS();

	//Signal the UART Tx thread that the hardware has finished sending a byte.
	Semaphore_Signal(UART2Tx);//Note null references are handled internally
}

// ----------------------------------------
// Signal_CreateADCSignal
// 
//
// Input:
//	semaphore: A semaphore that will be signalled every time 
//			the ADC completes a sample
// Output:
//  true:	If the signal has been successfully setup
//	fals:	The semaphores is a null reference
//
BOOL Signal_CreateADCSignal(bsem_Handle semaphore)
{
	if(semaphore == NULL)
		return pdFALSE;

	ADC = semaphore;

	return pdTRUE;
}

// ----------------------------------------
// Signal_CreateUART2Signal
// 
// Sets up signaling for the UART.
//
// Input:
//	RxSem: A semaphore that will be signalled every time 
//			the UART receives a byte
//	TxSem: A semaphore that willbe signalled every time
//			the UART finishes sending a byte.
// Output:
//  true:	If the signal has been successfully setup
//	fals:	If one of the semaphores is a null reference
// Conditions:
// 	none
BOOL Signal_CreateUART2Signal(bsem_Handle RxSem, bsem_Handle TxSem)
{
	if( RxSem == NULL || TxSem == NULL ) 
		return pdFALSE;

	UART2Rx = RxSem;
	UART2Tx = TxSem;
	
	return pdTRUE;
}
